package com.bootdo.friendcircle.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.friendcircle.domain.MessagePicDO;
import com.bootdo.friendcircle.service.MessagePicService;
import com.bootdo.system.domain.CompanyPictureDO;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lzy
 * @email 599048244@qq.com
 * @date 2018-06-09 09:33:47
 */
 
@Controller
@RequestMapping("/friendcircle/messagePic")
public class MessagePicController {
	@Autowired
	private MessagePicService messagePicService;
	
	@GetMapping()
	@RequiresPermissions("friendcircle:messagePic:messagePic")
	String MessagePic(){
	    return "friendcircle/messagePic/messagePic";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("friendcircle:messagePic:messagePic")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MessagePicDO> messagePicList = messagePicService.list(query);
		int total = messagePicService.count(query);
		PageUtils pageUtils = new PageUtils(messagePicList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("friendcircle:messagePic:add")
	String add(){
	    return "friendcircle/messagePic/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		MessagePicDO messagePic = messagePicService.get(id);
		model.addAttribute("messagePic", messagePic);
	    return "friendcircle/messagePic/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("friendcircle:messagePic:add")
	public R save( MessagePicDO messagePic){
		if(messagePicService.save(messagePic)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( MessagePicDO messagePic){
		messagePicService.update(messagePic);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(Integer id){
		if(messagePicService.remove(id)>0){
			MessagePicDO messagePicDO = messagePicService.get(id);
			String imgUrl1=messagePicDO.getUrl().substring(45, 62);
			OssUtil.deleteBucketInfo(imgUrl1);
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("friendcircle:messagePic:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		messagePicService.batchRemove(ids);
		return R.ok();
	}
	
}
